#!/bin/sh
#Barry Kauler Dec. 2010, license GPL v3 /usr/share/doc/legal.
#based on a script by technosaurus, Dec. 2010.

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

export TEXTDOMAIN=remove_builtin
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}

Yes_lbl="$(gettext 'Yes')"
No_lbl="$(gettext 'No')"

MSG1="`gettext \"Simple utility to 'delete' packages that are builtin\nto the read-only .sfs file (Squashfs filesystem)\"`"

D=/root/.packages/builtin_files

while [ 1 ];do
 FIXMENU='no'
 PKGS=`ls -1 $D`
 PKG=`Xdialog --left --screen-center --backtitle "${MSG1}" --title "$(gettext 'Remove builtin packages')" --stdout --help "$(gettext "In all modes of running Puppy, other than a full hard-drive installation,\n
all of the Puppy files are in a compressed read-only file named 'puppy.sfs'\n
or 'wary_500.sfs' (or some similar name). So you can't actually delete these\n
files. However, if you want to remaster the live-CD to create your own custom\n
Puppy (see Setup menu), you can use this little program to 'pretend' to delete\n
them -- and they will really be gone in the remastered CD. So, for example if\n
you remove SeaMonkey, it will be gone in the remastered CD thus reducing the\n
size of the .iso live-CD file.\n\n
Technical note: the lists of builtin files is at /root/.packages/builtin_files,\n
        the list of builtin pkgs is in /root/.packages/woof-installed-packages.")" --combobox "$(gettext 'Select package to remove:')" 0 0 $PKGS`
 if [ "$PKG" ];then
  aREGEX="|${PKG}|"
  DESCR="`grep "$aREGEX" /root/.packages/woof-installed-packages |cut -d "|" -f 10`"
  Xdialog --left --screen-center --backtitle "$(gettext 'Confirm that you want to delete') '${PKG}'" --title "$(gettext 'Remove builtin packages')" --ok-label "$Yes_lbl" --cancel-label "$No_lbl" --yesno "$(gettext 'Description of package:')\n
${DESCR}\n\n
$(gettext 'For information only, these are dependencies of') '${PKG}':\n
`grep "$aREGEX" /root/.packages/woof-installed-packages |cut -d "|" -f 9 | sed -e 's%^+%%' -e 's%,+% %g'`
\n
`eval_gettext \"Warning, removing '\\\${PKG}' _may_ break the following packages:\"`\n
`grep +${PKG} /root/.packages/woof-installed-packages |cut -d "|" -f 2 |tr "\n" " "`
\n
$(gettext 'Continue?')" 0 0
  if [ $? -eq 0 ];then
   [ "`grep '\.desktop$' ${D}/${PKG}`" != "" ] && FIXMENU='yes' #101222
   while read x
   do
    rm "$x"
   done < $D/$PKG
   rm $D/$PKG
   sed -i "\%|${PKG}|%d" /root/.packages/woof-installed-packages
   MSG1="`eval_gettext \"Package '\\\${PKG}' was removed. Choose another...\"`"
   if [ "$FIXMENU" = "yes" ];then #101222
    fixmenus
    [ "`pidof jwm`" != "" ] && jwm -restart
    sleep 1
   fi
  else
   MSG1="`eval_gettext \"Package '\\\${PKG}' was not removed. Choose another...\"`"
  fi
  continue
 fi
 break
done
